package com.iz0000.exp.ssmeh.conf;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import org.springframework.cache.CacheManager;
import org.springframework.cache.guava.GuavaCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @Auther: guoxl
 * @Date: 2019/4/7 22:06
 * @Description: 自定义缓存
 */
@Configuration
public class GuavaCacheConfig {

    @Bean
    public CacheManager cacheManager() {
        GuavaCacheManager cacheManager = new GuavaCacheManager();

        CacheBuilder cacheBuilder = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).
                maximumSize(1000).removalListener(removalNotification -> {
                    System.out.println(removalNotification.getKey());
                    System.out.println(removalNotification.getValue());
                    System.out.println(removalNotification.getCause());
                });

        cacheManager.setCacheBuilder(cacheBuilder);
        return cacheManager;
    }

}
